/*
 * Copyright 2010 original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alertleon;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.util.Assert;

public class AlertConfiguration {

    static final String ENABLED_PROP = "alert.enabled";
    static final String DELAY_PROP = "alert.delay";
    static final String PORT_PROP = "alert.port";
    static final String SMTP_SERVER_PROP = "alert.smtpServer";
    static final String TO_ADDRESS_PROP = "alert.toAddr";
    static final String FROM_ADDRESS_PROP = "alert.fromAddr";
    static final String SUBJECT_PROP = "alert.subject";
    static final String EXCLUDE_CLASS_PROP = "alert.excludeClass";

    private final boolean enabled;
    private final int delay;
    private final int port;
    private final String smtpServer;
    private final String fromAddress;
    private final String toAddress;
    private final String subject;
    private final String excludeClasses;

    public AlertConfiguration(AlertConfigurationBuilder builder) {
        Assert.notNull(builder);
        this.enabled = builder.enabled();
        this.delay = builder.delay();
        this.port = builder.port();
        this.fromAddress = builder.fromAddress();
        this.smtpServer = builder.smtpServer();
        this.subject = builder.subject();
        this.toAddress = builder.toAddress();
        this.excludeClasses = builder.excludeClasses();
    }

    boolean isEnabled() {
        return enabled;
    }

    int getDelay() {
        return delay;
    }

    int getPort() {
        return port;
    }

    String getSmtpServer() {
        return smtpServer;
    }

    String getToAddress() {
        return toAddress;
    }

    String getFromAddress() {
        return fromAddress;
    }

    String getSubject() {
        return subject;
    }

    List<String> getExcludeClasses() {
        List<String> returnList = new ArrayList<String>();

        if (StringUtils.hasText(excludeClasses)) {
            String[] excludeClassesArray = excludeClasses.replaceAll("\\s+", "").split(",");
            returnList = Arrays.asList(excludeClassesArray);
        }

        return returnList;
    }
}
